package com.boss.salary.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IPersonBatchAddDao;
import com.boss.salary.vo.ProvinceInfoVo;
import com.boss.sys.utils.SysUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Repository
public class PersonBatchAddDaoImpl extends BaseDaoImpl implements IPersonBatchAddDao {

    /**
     * 区划初始化数据
     *
     * @param provinceInfoVos
     */
    @Override
    public void batchSaveProvinceInfo(List<ProvinceInfoVo> provinceInfoVos) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        String createTime = String.valueOf(DateUtils.getCurrentTimeStamp());

        List<Object[]> paramList1 = new ArrayList<>(provinceInfoVos.size());
        Class<?> clazz = ProvinceInfoVo.class;
        for (ProvinceInfoVo provinceInfoVo : provinceInfoVos) {
            Object[] paramArr = {
                    SysUtils.generateGuid(), createTime, createTime,
                    provinceInfoVo.getAgencyId(), provinceInfoVo.getPersonId(), provinceInfoVo.getMofDivCode()
            };
            List<Object> params = new ArrayList<>(Arrays.asList(paramArr));
            for (int i = 1; i < 101; i++) {
                Method getMethod = clazz.getDeclaredMethod("getS" + i);
                params.add(getMethod.invoke(provinceInfoVo));
            }
            paramList1.add(params.toArray());
        }

        /**
         * 插入sal_salary_month_end表数据
         */
        StringBuilder sb2 = new StringBuilder();
        sb2.append(" insert into ").append(TableName.SALARY_MONTH_END)
                .append(" ( id, create_time, update_time, agency_id, person_id,is_thirteen, mof_div_code, ")
                .append(" s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,  ")
                .append(" s21, s22, s23, s24, s25, s26, s27, s28, s29, s30, s31, s32, s33, s34, s35, s36, s37, s38, s39, s40, ")
                .append(" s41, s42, s43, s44, s45, s46, s47, s48, s49, s50, s51, s52, s53, s54, s55, s56, s57, s58, s59, s60, ")
                .append(" s61, s62, s63, s64, s65, s66, s67, s68, s69, s70, s71, s72, s73, s74, s75, s76, s77, s78, s79, s80, ")
                .append(" s81, s82, s83, s84, s85, s86, s87, s88, s89, s90, s91, s92, s93, s94, s95, s96, s97, s98, s99, s100 ) ")
                .append(" values ( ?, ?, ?, ?, ?, 0 ,?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ");
        super.getJdbcTemplate().batchUpdate(sb2.toString(), new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return paramList1.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = paramList1.get(i);
                for(int a = 0; a < 106; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });

        /**
         * 插入sal_person_info表数据
         */
        List<Object[]> paramList2 = new ArrayList<>(provinceInfoVos.size());
        for (ProvinceInfoVo provinceInfoVo : provinceInfoVos) {
            Object[] paramArr = {
                    provinceInfoVo.getPersonId(), createTime, createTime,
                    provinceInfoVo.getPersonName(), provinceInfoVo.getSex(), "01", provinceInfoVo.getIdenNo(), "CN",
                    provinceInfoVo.getState(), getBirthday(provinceInfoVo.getIdenNo()), SystemDictConstant.IS_FALSE, provinceInfoVo.getMofDivCode()
            };
            List<Object> params = new ArrayList<>(Arrays.asList(paramArr));
            paramList2.add(params.toArray());
        }
        StringBuilder sql2 = new StringBuilder();
        sql2.append("INSERT INTO ").append(TableName.PERSON_INFO)
                .append("(id, create_time, update_time, person_name, sex_code, iden_type_code, iden_no, ")
                .append("nation, state, birthday,is_deleted,mof_div_code ) ")
                .append("VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
        super.getJdbcTemplate().batchUpdate(sql2.toString(), new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return paramList2.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = paramList2.get(i);
                for(int a = 0; a < 12; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });

        /**
         * 插入sal_person_ext表数据
         */
        List<Object[]> paramList3 = new ArrayList<>(provinceInfoVos.size());
        for (ProvinceInfoVo provinceInfoVo : provinceInfoVos) {
            Object[] paramArr = {
                    SysUtils.generateGuid(), createTime, createTime, provinceInfoVo.getPersonId(),
                    provinceInfoVo.getPersonName().trim(), provinceInfoVo.getAgencyId(),
                    provinceInfoVo.getAgencyCode(), provinceInfoVo.getIdenNo(), provinceInfoVo.getState(),//20210304新增
                    provinceInfoVo.getSalaCardNo(), provinceInfoVo.getSalaCardBank(),
                    provinceInfoVo.getMeritPayCode(), "2", SystemDictConstant.IS_FALSE, SystemDictConstant.IS_FALSE, provinceInfoVo.getMofDivCode(),
                    provinceInfoVo.getShowOrder(),provinceInfoVo.getPersonnelCode()
            };
            List<Object> params = new ArrayList<>(Arrays.asList(paramArr));
            paramList3.add(params.toArray());
        }
        StringBuilder sql3 = new StringBuilder();
        sql3.append("insert into ").append(TableName.PERSON_EXT)
                .append("(id, create_time, update_time, person_id,person_name,agency_id,")
                .append("agency_code,iden_no,state,sala_card_no,sala_card_bank,merit_pay_code,")
                .append("verify_state,is_stop_salary,is_deleted,mof_div_code,show_order,personnel_code) ")
                .append("values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        super.getJdbcTemplate().batchUpdate(sql3.toString(), new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return paramList3.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = paramList3.get(i);
                for(int a = 0; a < 18; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });


        /**
         * 插入sal_person_operate表数据
         */
        List<Object[]> paramList5 = new ArrayList<>(provinceInfoVos.size());
        for (ProvinceInfoVo provinceInfoVo : provinceInfoVos) {
            Object[] paramArr = new Object[]{
                    SysUtils.generateGuid(), createTime, createTime, provinceInfoVo.getPersonId(),
                    provinceInfoVo.getPersonName().trim(), provinceInfoVo.getSex(), "01", "add",
                    provinceInfoVo.getIdenNo().trim(), "CN", getBirthday(provinceInfoVo.getIdenNo()),
                    provinceInfoVo.getState(), "0", provinceInfoVo.getAgencyId(), provinceInfoVo.getAgencyCode(),
                    provinceInfoVo.getSalaCardNo(), provinceInfoVo.getSalaCardBank(), "2021-01-01",
                    provinceInfoVo.getMeritPayCode(), "2", SystemDictConstant.IS_FALSE, "1",
                    provinceInfoVo.getSalaryMonth(), provinceInfoVo.getMofDivCode(),
                    provinceInfoVo.getShowOrder(),provinceInfoVo.getPersonnelCode()
            };
            List<Object> params = new ArrayList<>(Arrays.asList(paramArr));
            paramList5.add(params.toArray());
        }
        StringBuilder sql5 = new StringBuilder();
        sql5.append("insert into ").append(TableName.PERSON_OPERATE)
                .append("(id, create_time, update_time, person_id, person_name, sex_code, iden_type_code,")
                .append("operate_code, iden_no, nation,birthday, state, iden_is_verify,agency_id,")
                .append("agency_code,sala_card_no,sala_card_bank, start_date, merit_pay_code,verify_state,")
                .append("is_deleted ,is_real,salary_month, mof_div_code,show_order,personnel_code) ");
        sql5.append("values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        super.getJdbcTemplate().batchUpdate(sql5.toString(), new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return paramList5.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = paramList5.get(i);
                for(int a = 0; a < 26; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });
    }

    /**
     * 根据身份证号获取出生日期  yyyy-MM-dd
     * @param IDCard
     * @return
     */
    public static String getBirthday(String IDCard) {
        String birthday = "";
        String year = "";
        String month = "";
        String day = "";
        if (StringUtils.isNotBlank(IDCard)) {
            //15位身份证号
            if (IDCard.length() == 15) {
                // 身份证上的年份(15位身份证为1980年前的)
                year = "19" + IDCard.substring(6, 8);
                //身份证上的月份
                month = IDCard.substring(8, 10);
                //身份证上的日期
                day = IDCard.substring(10, 12);
                //18位身份证号
            } else if (IDCard.length() == 18) {
                // 身份证上的年份
                year = IDCard.substring(6).substring(0, 4);
                // 身份证上的月份
                month = IDCard.substring(10).substring(0, 2);
                //身份证上的日期
                day = IDCard.substring(12).substring(0, 2);
            }
            birthday = year + "-" + month + "-" + day;
        }
        return birthday;
    }

}
